home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / Python 1.3 PPC / Lib / img / imgjpeg.py < prev    next >
Encoding:
Python Source  |  1995-10-11  |  3.5 KB  |  117 lines  |  [TEXT/PYTH]

  1. """A module that reads JPEG files using the SGI Compression Library.
  2.  
  3. This module should only be used in a python-interpreter with the cl
  4. module configured, otherwise the builtin imgjpeg module is better."""
  5.  
  6. import cl
  7. from imgformat import rgb, rgb_b2t, xrgb8, xrgb8_b2t, xgrey, xgrey_b2t, \
  8.               rgb8, rgb8_b2t, grey, grey_b2t
  9.  
  10. error = 'imgpjpeg.error'
  11.  
  12. class reader:
  13.     def __init__(self, filename):
  14.     self._filename = filename
  15.     self._file = f = open(filename, 'rb')
  16.     header = f.read(16)
  17.     scheme = cl.QueryScheme(header)
  18.     self._decompressor = decomp = cl.OpenDecompressor(scheme)
  19.     size = cl.QueryMaxHeaderSize(scheme)
  20.     if size > len(header):
  21.         header = header + f.read(size - len(header))
  22.     headersize = decomp.ReadHeader(header)
  23.     self.width = decomp.GetParam(cl.IMAGE_WIDTH)
  24.     self.height = decomp.GetParam(cl.IMAGE_HEIGHT)
  25.     self.format_choices = rgb, rgb_b2t, xrgb8, xrgb8_b2t, xgrey, xgrey_b2t
  26.     self.format = rgb        # default format
  27.  
  28.     def args(self):
  29.     return self.__dict__
  30.     
  31.     def read(self):
  32.     if self.format in (rgb, rgb_b2t):
  33.         original_format = cl.RGBX
  34.     elif self.format in (xrgb8, xrgb8_b2t, rgb8, rgb8_b2t):
  35.         original_format = cl.RGB332
  36.     elif self.format in (xgrey, xgrey_b2t, grey, grey_b2t):
  37.         original_format = cl.GRAYSCALE
  38.     else:
  39.         raise error, 'Unknown format'
  40.     if self.format in (rgb_b2t, xrgb8_b2t, xgrey_b2t, rgb8_b2t, grey_b2t):
  41.         orientation = cl.BOTTOM_UP
  42.     else:
  43.         orientation = cl.TOP_DOWN
  44.     width = self.width
  45.     if original_format == cl.RGB332 and hasattr(cl, 'JPEG_SOFTWARE'):
  46.         # Irix 5.3 uses more buffer than it should, so allocate some extra
  47.         width = (width + 3) & ~3
  48.     params = [cl.ORIGINAL_FORMAT, original_format,
  49.           cl.ORIENTATION, orientation,
  50.           cl.FRAME_BUFFER_SIZE,
  51.            width*self.height*cl.BytesPerPixel(original_format)]
  52.     self._decompressor.SetParams(params)
  53.     self._file.seek(0)
  54.     data = self._decompressor.Decompress(1, self._file.read())
  55.     if original_format == cl.RGB332 and hasattr(cl, 'JPEG_SOFTWARE'):
  56.         # only return the image, not the extra space
  57.         return data[:self.width*self.height]
  58.     else:
  59.         return data
  60.  
  61.     def write(self, data):
  62.     raise error, 'Cannot write() to reader'
  63.  
  64. class writer:
  65.     def __init__(self, filename):
  66.     self._filename = filename
  67.     self.format_choices = rgb, rgb_b2t, xrgb8, xrgb8_b2t, xgrey, xgrey_b2t
  68.     self.format = rgb
  69.  
  70.     def args(self):
  71.     return self.__dict__
  72.     
  73.     def _get(self, attr):
  74.     try:
  75.         return getattr(self, attr)
  76.     except AttributeError:
  77.         raise error, "Required attribute '%s' missing"%attr
  78.  
  79.     def read(self):
  80.     raise error, 'Cannot read() from writer'
  81.  
  82.  
  83.     def write(self, data):
  84.     width = self._get('width')
  85.     height = self._get('height')
  86.     format = self._get('format')
  87.     if format in (rgb, rgb_b2t):
  88.         original_format = cl.RGBX
  89.     elif format in (xrgb8, xrgb8_b2t):
  90.         original_format = cl.RGB332
  91.     elif format in (xgrey, xgrey_b2t):
  92.         original_format = cl.GRAYSCALE
  93.     else:
  94.         raise error, 'Unknown format'
  95.     if format in (rgb_b2t, xrgb8_b2t, xgrey_b2t):
  96.         orientation = cl.BOTTOM_UP
  97.     else:
  98.         orientation = cl.TOP_DOWN
  99.     if width * height * cl.BytesPerPixel(original_format) != len(data):
  100.         raise error, 'Incorrect datasize'
  101.     params = [cl.ORIGINAL_FORMAT, original_format,
  102.           cl.ORIENTATION, orientation,
  103.           cl.IMAGE_WIDTH, width,
  104.           cl.IMAGE_HEIGHT, height]
  105.     try:
  106.         quality = self.quality
  107.     except AttributeError:
  108.         pass
  109.     else:
  110.         params.append(cl.QUALITY_FACTOR)
  111.         params.append(quality)
  112.     compressor = cl.OpenCompressor(cl.JPEG)
  113.     compressor.SetParams(params)
  114.     data = compressor.Compress(1, data)
  115.     file = open(self._filename, 'wb')
  116.     file.write(data)
  117.